package com.punai.dataanalysis.epdatakb.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.page.PageDomain;
import com.punai.common.core.page.TableSupport;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.service.NG001change1212.NG001YearGclPdC1212Service;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.bo.OtherColumn;
import com.punai.dataanalysis.ng001.bo.YearLjWccz;
import com.punai.dataanalysis.ng001.domain.ProjectTable;
import com.punai.dataanalysis.ng001.mapper.ProjectTableMapper;
import com.punai.dataanalysis.ng001.mapper.YearGclPdMapper;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import com.punai.prodatalibrary.mapper.LibProjectMapper;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.dataanalysis.epdatakb.mapper.DataAnalysisNdgclpdqkhzbMapper;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgclpdqkhzb;
import com.punai.dataanalysis.epdatakb.service.IDataAnalysisNdgclpdqkhzbService;

/**
 * 年度工程量盘点情况汇总Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-20
 */
@Service
public class DataAnalysisNdgclpdqkhzbServiceImpl extends ServiceImpl<DataAnalysisNdgclpdqkhzbMapper,DataAnalysisNdgclpdqkhzb> implements IDataAnalysisNdgclpdqkhzbService
{
    @Autowired
    private DataAnalysisNdgclpdqkhzbMapper dataAnalysisNdgclpdqkhzbMapper;

    @Autowired
    private ProjectTableMapper projectTableMapper;
    @Autowired
    private YearGclPdMapper yearGclPdMapper;

    @Autowired
    private ILibProjectInfoService libProjectInfoService;

    @Autowired
    private NG001YearGclPdC1212Service ng001YearGclPdC1212Service;

    @Override
    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        // c1212处理
        if (isC1212) {
            List<? extends BaseEntity> dataList = ng001YearGclPdC1212Service.getDataList(dataAnalysisVo);
            return dataList;
        }

        List<GlobalProInfoBo> list = libProjectInfoService.selectInfoListNoPerm(new GlobalProInfoBo());

        List<String> pmInfoIds = list.stream().map(GlobalProInfoBo::getPmproinfoid).collect(Collectors.toList());
        List<ProjectTable> projectTables = projectTableMapper.selectNdgclpdqkhzbListByVo(pmInfoIds);
        pmInfoIds = projectTables.stream().map(ProjectTable::getPhid).filter(Objects::nonNull).collect(Collectors.toList());
        if (CollUtil.isEmpty(pmInfoIds)) {
            return new ArrayList<>();
        }
        Map<String, ProjectTable> projectTableMap = projectTables.stream().collect(Collectors.toMap(ProjectTable::getPhid, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> yearWccz = getYearWccz(pmInfoIds);
        Map<String, YearLjWccz> yearWcczMap = yearWccz.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> ljWccz = getLjWccz(pmInfoIds);
        Map<String, YearLjWccz> ljWcczMap = ljWccz.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<OtherColumn> other = getOther(pmInfoIds);
        Map<String, OtherColumn> otherMap = other.stream().collect(Collectors.toMap(OtherColumn::getPc, Function.identity(), (v1, v2) -> v2));

        List<DataAnalysisNdgclpdqkhzb> res = new ArrayList<>();
        for (GlobalProInfoBo globalProInfoBo : list) {
            DataAnalysisNdgclpdqkhzb dn = new DataAnalysisNdgclpdqkhzb();
            if(StrUtil.isNotEmpty(globalProInfoBo.getPmproinfoid())){
                String pc = globalProInfoBo.getPmproinfoid();
                ProjectTable projectTable = projectTableMap.get(pc);
                if (projectTable == null) {
                    continue;
                }
                dn = DataAnalysisNdgclpdqkhzb.conversion(dn,projectTable);
                YearLjWccz wccz = yearWcczMap.get(pc);
                YearLjWccz ljwccz = ljWcczMap.get(pc);
                OtherColumn otherColumn = otherMap.get(pc);

                dn.setYearWccz(wccz.getPdzhj());
                dn.setYearWcczSkYbjs(wccz.getSkybjs());
                dn.setYearWcczSkJyjs(wccz.getSkjyjs());
                dn.setYearWcczJgc(wccz.getJgc());

                dn.setKlpdz(ljwccz.getPdzhj());
                dn.setKlWcczSkYbjs(ljwccz.getSkybjs());
                dn.setKlWcczSkJyjs(ljwccz.getSkjyjs());
                dn.setKlWcczJgc(ljwccz.getJgc());

                dn.setBnkjqrsr(otherColumn.getBnkjqrsr());
                dn.setXxjd(otherColumn.getXxjd());

            }
            res.add(dn);
        }
        DataAnalysisNdgclpdqkhzb yjs = fzXj(res, "竣工已结算");
        yjs.setProName("竣工已结算工程小计");
        DataAnalysisNdgclpdqkhzb wjs = fzXj(res, "竣工未结算");
        wjs.setProName("竣工未结算工程小计");
        DataAnalysisNdgclpdqkhzb zjxm = fzXj(res, "在建项目");
        zjxm.setProName("在建项目工程小计");
        DataAnalysisNdgclpdqkhzb zj = zj(res);
        zj.setProName("合计");

        // 重组 按照项目部
        res = xmbFz(res);
        res.add(yjs);
        res.add(wjs);
        res.add(zjxm);
        res.add(zj);
        return res;
    }

    private List<DataAnalysisNdgclpdqkhzb> xmbFz(List<DataAnalysisNdgclpdqkhzb> res) {
        List<DataAnalysisNdgclpdqkhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdgclpdqkhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getXmb())).collect(Collectors.groupingBy(DataAnalysisNdgclpdqkhzb::getXmb));
        for (Map.Entry<String, List<DataAnalysisNdgclpdqkhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdgclpdqkhzb> value = entry.getValue();
            newResList.addAll(value);
            DataAnalysisNdgclpdqkhzb zj = zj(value);
            zj.setProName(key  + "小计");
            newResList.add(zj);
        }
        return newResList;
    }

    /**
     * 本年完成产值
     * @param pmInfoIds
     * @return
     */
    private List<YearLjWccz> getYearWccz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        if (CollUtil.isNotEmpty(pmInfoIds)) {
            List<JtczResultBo> yearwcczhj = yearGclPdMapper.yearwcczhj(pmInfoIds);
            Map<String, BigDecimal> yearwcczhjMap = yearwcczhj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> yearwcczybjs = yearGclPdMapper.yearwcczybjs(pmInfoIds);
            Map<String, BigDecimal> yearwcczybjsMap = yearwcczybjs.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> yearwcczjyjs = yearGclPdMapper.yearwcczjyjs(pmInfoIds);
            Map<String, BigDecimal> yearwcczjyjsMap = yearwcczjyjs.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> yearwcczjgc = yearGclPdMapper.yearwcczjgc(pmInfoIds);
            Map<String, BigDecimal> yearwcczjgcMap = yearwcczjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            for (String pc : pmInfoIds) {
                YearLjWccz wccz = new YearLjWccz();
                wccz.setPc(pc);
                wccz.setPdzhj(yearwcczhjMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setSkybjs(yearwcczybjsMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setSkjyjs(yearwcczjyjsMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setJgc(yearwcczjgcMap.getOrDefault(pc,BigDecimal.ZERO));
                resList.add(wccz);
            }
        }
        return resList;
    }
    /**
     * 开累完成产值
     * @param pmInfoIds
     * @return
     */
    private List<YearLjWccz> getLjWccz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        if (CollUtil.isNotEmpty(pmInfoIds)) {
            List<JtczResultBo> ljwcczhj = yearGclPdMapper.ljwcczhj(pmInfoIds);
            Map<String, BigDecimal> ljwcczhjMap = ljwcczhj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> ljwcczybjs = yearGclPdMapper.ljwcczybjs(pmInfoIds);
            Map<String, BigDecimal> ljwcczybjsMap = ljwcczybjs.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> ljwcczjyjs = yearGclPdMapper.ljwcczjyjs(pmInfoIds);
            Map<String, BigDecimal> ljwcczjyjsMap = ljwcczjyjs.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> ljwcczjgc = yearGclPdMapper.ljwcczjgc(pmInfoIds);
            Map<String, BigDecimal> ljwcczjgcMap = ljwcczjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            for (String pc : pmInfoIds) {
                YearLjWccz wccz = new YearLjWccz();
                wccz.setPc(pc);
                wccz.setPdzhj(ljwcczhjMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setSkybjs(ljwcczybjsMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setSkjyjs(ljwcczjyjsMap.getOrDefault(pc,BigDecimal.ZERO));
                wccz.setJgc(ljwcczjgcMap.getOrDefault(pc,BigDecimal.ZERO));
                resList.add(wccz);
            }

        }
        return resList;
    }
    /**
     * 其他列
     * @param pmInfoIds
     * @return
     */
    private List<OtherColumn> getOther(List<String> pmInfoIds) {
        List<OtherColumn> resList = new ArrayList<>();
        if (CollUtil.isNotEmpty(pmInfoIds)) {
            List<JtczResultBo> kjqrsr = yearGclPdMapper.kjqrsr(pmInfoIds);
            Map<String, BigDecimal> kjqrsrMap = kjqrsr.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
            List<JtczResultBo> xxjd = projectTableMapper.xxjd(pmInfoIds);
            Map<String, String> xxjdMap = xxjd.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVals, (v1, v2) -> v2));
            for (String pc : pmInfoIds) {
                OtherColumn oc = new OtherColumn();
                oc.setPc(pc);
                oc.setBnkjqrsr(kjqrsrMap.getOrDefault(pc,BigDecimal.ZERO));
                oc.setXxjd(xxjdMap.getOrDefault(pc,""));
                resList.add(oc);
            }
        }
        return resList;
    }

    // 分组 小计
    //  已结算工程小计
    //  竣工未结算工程小计
    //  在建工程小计

    private DataAnalysisNdgclpdqkhzb fzXj(List<DataAnalysisNdgclpdqkhzb> resList,String proStatus) {
        List<DataAnalysisNdgclpdqkhzb> collect = resList.stream().filter(e -> StrUtil.equals(e.getProStatus(), proStatus)).collect(Collectors.toList());
        return zj(collect);
    }

    private DataAnalysisNdgclpdqkhzb zj(List<DataAnalysisNdgclpdqkhzb> resList) {
        DataAnalysisNdgclpdqkhzb zj = new DataAnalysisNdgclpdqkhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal yearWccz = BigDecimal.ZERO;
        BigDecimal yearWcczZyfb = BigDecimal.ZERO;
        BigDecimal yearWcczSkYbjs = BigDecimal.ZERO;
        BigDecimal yearWcczSkJyjs = BigDecimal.ZERO;
        BigDecimal yearWcczJgc = BigDecimal.ZERO;
        BigDecimal ljkgpdz = BigDecimal.ZERO;
        BigDecimal ljjdhpdz = BigDecimal.ZERO;
        BigDecimal klpdz = BigDecimal.ZERO;
        BigDecimal klpdzZyfb = BigDecimal.ZERO;
        BigDecimal klWcczSkYbjs = BigDecimal.ZERO;
        BigDecimal klWcczSkJyjs = BigDecimal.ZERO;
        BigDecimal klWcczJgc = BigDecimal.ZERO;
        BigDecimal jdBeforeKjQrsr = BigDecimal.ZERO;
        BigDecimal jdAfterKjQrsr = BigDecimal.ZERO;
        BigDecimal yearBeforeLjQrsr = BigDecimal.ZERO;
        BigDecimal bnkjqrsr = BigDecimal.ZERO;
        BigDecimal sygcl = BigDecimal.ZERO;
        for (DataAnalysisNdgclpdqkhzb gcl : resList) {
            htTotal = NumberUtil.add(gcl.getHtTotal(),htTotal);
            yearWccz = NumberUtil.add(gcl.getYearWccz(),yearWccz);
            yearWcczZyfb = NumberUtil.add(gcl.getYearWcczZyfb(),yearWcczZyfb);
            yearWcczSkYbjs = NumberUtil.add(gcl.getYearWcczSkYbjs(),yearWcczSkYbjs);
            yearWcczSkJyjs = NumberUtil.add(gcl.getYearWcczSkJyjs(),yearWcczSkJyjs);
            yearWcczJgc = NumberUtil.add(gcl.getYearWcczJgc(),yearWcczJgc);
            ljkgpdz = NumberUtil.add(gcl.getLjkgpdz(),ljkgpdz);
            ljjdhpdz = NumberUtil.add(gcl.getLjjdhpdz(),ljjdhpdz);
            klpdz = NumberUtil.add(gcl.getKlpdz(),klpdz);
            klpdzZyfb = NumberUtil.add(gcl.getKlpdzZyfb(),klpdzZyfb);
            klWcczSkYbjs = NumberUtil.add(gcl.getKlWcczSkYbjs(),klWcczSkYbjs);
            klWcczSkJyjs = NumberUtil.add(gcl.getKlWcczSkJyjs(),klWcczSkJyjs);
            klWcczJgc = NumberUtil.add(gcl.getKlWcczJgc(),klWcczJgc);
            jdBeforeKjQrsr = NumberUtil.add(gcl.getJdBeforeKjQrsr(),jdBeforeKjQrsr);
            jdAfterKjQrsr = NumberUtil.add(gcl.getJdAfterKjQrsr(),jdAfterKjQrsr);
            yearBeforeLjQrsr = NumberUtil.add(gcl.getYearBeforeLjQrsr(),yearBeforeLjQrsr);
            bnkjqrsr = NumberUtil.add(gcl.getBnkjqrsr(),bnkjqrsr);
            sygcl = NumberUtil.add(gcl.getSygcl(),sygcl);
        }
        zj.setHtTotal(         htTotal );
        zj.setYearWccz(        yearWccz );
        zj.setYearWcczZyfb(    yearWcczZyfb );
        zj.setYearWcczSkYbjs(  yearWcczSkYbjs );
        zj.setYearWcczSkJyjs(  yearWcczSkJyjs );
        zj.setYearWcczJgc(     yearWcczJgc );
        zj.setLjkgpdz(         ljkgpdz );
        zj.setLjjdhpdz(        ljjdhpdz );
        zj.setKlpdz(           klpdz );
        zj.setKlpdzZyfb(       klpdzZyfb );
        zj.setKlWcczSkYbjs(    klWcczSkYbjs );
        zj.setKlWcczSkJyjs(    klWcczSkJyjs );
        zj.setKlWcczJgc(       klWcczJgc );
        zj.setJdBeforeKjQrsr(  jdBeforeKjQrsr );
        zj.setJdAfterKjQrsr(   jdAfterKjQrsr );
        zj.setYearBeforeLjQrsr(yearBeforeLjQrsr );
        zj.setBnkjqrsr(        bnkjqrsr );
        zj.setSygcl(           sygcl );
        zj.hb();
        return zj;
    }

}
